package org.ilovecode.display {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class ImageView extends Screen {
		
		//{ region Stage elements
		
		public var closeButton:SimpleButton;
		
		//} endregion
		
		private var _lastScreen:Screen;
		private var _image:DisplayObject;
		
		public function ImageView(image:DisplayObject = null, lastScreen:Screen = null) {
			//this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			super(ScreenName.IMAGE_VIEW, "", "imageView");
			
			_image = image;
			_lastScreen = lastScreen;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			var scaleFactorX:Number = 1;
			var scaleFactorY:Number = 1;
			
			if (_image.width > this.width) {
				scaleFactorX = this.width / _image.width;
			}
			if (_image.height > this.height) {
				scaleFactorY = this.height / _image.height;
			}
			
			_image.scaleX = _image.scaleY = (scaleFactorX < scaleFactorY) ? scaleFactorX : scaleFactorY;
			
			_image.x = (this.width - _image.width) / 2;
			_image.y = (this.height - _image.height) / 2;
			
			this.addChildAt(_image, 1);
		}
		
		
		override protected function addListeners():void
		{
			super.addListeners();
			closeButton.addEventListener(MouseEvent.CLICK, closeButtonClick);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			closeButton.removeEventListener(MouseEvent.CLICK, closeButtonClick);
		}
		
		
		/*private function addedToStage(event:Event):void {
			init();
		}
		
		private function init():void {
			initListeners();
		}
		
		public function showImage(image:DisplayObject):void {
			var scaleFactorX:Number = 1;
			var scaleFactorY:Number = 1;
			
			if (image.width > this.width) {
				scaleFactorX = this.width / image.width;
			}
			if (image.height > this.height) {
				scaleFactorY = this.height / image.height;
			}
			
			image.scaleX = image.scaleY = (scaleFactorX < scaleFactorY) ? scaleFactorX : scaleFactorY;
			
			image.x = (this.width - image.width) / 2;
			image.y = (this.height - image.height) / 2;
			
			this.addChildAt(image, 1);
		}
		
		private function initListeners():void {
			closeButton.addEventListener(MouseEvent.CLICK, closeButtonClick);
		}*/
		
		private function closeButtonClick(event:MouseEvent):void {
			//this.dispatchEvent(new Event(Event.COMPLETE));
			controller.setCurrentScreen(_lastScreen);
		}
	}
	
}